#include"Vector.h"
//int a=100;

//类的域作用限定符只能写在返回值后面，这是规定！！
// template<typename T>
// vector<T>:: iterator begin()
// {
//     return _start;
// }
// template<class T>
// vector<T>::iterator end()
// {
//     return _finish;
// }
// template<class T>
// vector<T>::const_iterator begin()const
// {
//     return _start;
// }
// template<class T>
// vector<T>::const_iterator end()const
// {
//     return _finish;
// }

template<class T>
vector<T>::vector()
{
    _start=new T();

    _finish=_end_of_storage=_start;
}
template<class T>
vector<T>::vector(const vector& v)
{
    _start=new T[v.capacity()];
    
    for(int i=0;i<v.size();i++)
        _start[i]=v[i];//!!!

    _finish=_start+v.size();
    _end_of_storage=_start+v.capacity();
}

template<class T> template<class InputIterator>
vector<T>::vector(InputIterator first,InputIterator last)//左闭右开
{
    int sz=last-first;
    _start=new T[sz];
    for(int i=0;i<sz;i++)
    {
        *(_start+i)=*(first+i);
    }
    _finish=_start+sz;
    _end_of_storage=_finish;
}


template<class T>
size_t vector<T>::size()const
{
    return _finish-_start;
}
template<class T>
size_t vector<T>::capacity()const
{
    return _end_of_storage-_start;
}

template<class T>
vector<T>::~vector()
{
    delete[] _start;
    _start=_finish=_end_of_storage=nullptr;
}
template<class T>
void vector<T>::reserve(size_t n)
{
    if(n>=capacity())
    {
        n=(n==0?4:n);//对capacity初始值为0情况做一个判断

        int sz=_finish-_start;
        T* new_space=new T[n];
        //memcpy(new_space,_start,sizeof(T)*size());//这里可能出现浅拷贝问题，所以用赋值
        for(int i=0;i<sz;i++)
            new_space[i]=_start[i];
        delete[] _start;

        _start=new_space;
        _finish=_start+sz;
        _end_of_storage=_start+n;
    }
}
template<class T>
void vector<T>::resize(size_t n,const T val)
{
    if(n>capacity())
        reserve(n);

    while(n>size() && _finish<_start+n)
    {
        *_finish=val;
        _finish++;
    }

    if(n<size())
        _finish=_start+n;
}

template<class T>
void vector<T>::push_back(const T& val)
{
    // if(_finish>_end_of_storage)
    //     reserve(capacity()*2);
    // *_finish=val;
    // _finish++;
    insert(_finish,val);
}
template<class T>
T* vector<T>::insert(iterator pos,const T& val)
{
    if(pos<_start || pos>_finish)
    {
        std::cout<<"数组越界访问！！"<<std::endl;
        return nullptr;
    }
    int len=pos-_start;//扩容导致pos变成野指针，所以需要记录下pos的相对位置

    if(_finish>=_end_of_storage)
        reserve(capacity()*2);

    pos=_start+len;

    iterator end=_finish;
    while(end>=pos)
    {
        *end=*(end-1);
        end--;
    }
    *pos=val;
    _finish++;

    return pos;
}
template<class T>
T* vector<T>::erase(iterator pos)
{
    // static int n=0;
    // std::cout<<"第"<<n<<"次循环..."<<std::endl;
    // n++;
    if(pos<_start || pos>=_finish)
    {
        std::cout<<"数组越界访问！！"<<pos-_start<<std::endl;
        return nullptr;
    }

    iterator key=pos;
    while(key+1<_finish)
    {
        *key=*(key+1);
        key++;
    }
    _finish--;
    return pos;
}
template<class T>
T* vector<T>::erase(iterator first,iterator last)
{
    if(first>last || first<_start || last>_finish)
    {
        std::cout<<"数组越界访问！！"<<std::endl;
        return nullptr;
    }
    int len=last-first;
    iterator pos=first;
    while(pos+len<_finish)
    {
        *pos=*(pos+len);
        pos++;
    }
    _finish-=len;

    return first;
}

template<class T>
T& vector<T>::operator[](size_t pos)
{
    if(pos<0 || pos>=size())
    {
        std::cout<<pos<<":越界访问!!"<<std::endl;
    }
    return *(_start+pos);
}
template<class T>
const T& vector<T>::operator[](size_t pos)const
{
    if(pos<0 || pos>=size())std::cout<<"越界访问!!"<<std::endl;
    return *(_start+pos);
}
template<class T>
void vector<T>::swap(vector& v)
{
    std::swap(_start,v._start);
    std::swap(_finish,v._finish);
    std::swap(_end_of_storage,v._end_of_storage);
}